'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 副本战斗记录
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var mongoose = require('mongoose');
var _ = require('lodash');
var autoIncrement = require('mongoose-auto-increment');
var luckItemSchema = require('./luck/luckItemSchema');

/**
 * 副本战斗记录
 */
var bossRecordSchema = new mongoose.Schema({
    // 进攻方角色id
    atk_role_id: {
        type: Number,
        required: true
    },

    // 攻击方数据
    atk: {
        hero: {},
        equip: {
            weapon: {},
            head: {},
            shoulder: {},
            armor: {},
            leg: {},
            necklace: {},
            ring: {},
            ear_ring: {}
        },
        soldier_list: [],
        skill_list: []
    },

    // 防守方数据
    def: {
        // 名字
        name: {
            type: String,
            default: ''
        },

        // 总共的金币
        total_gold: {
            type: Number
        },

        // 总共的经验
        total_exp: {
            type: Number
        },

        // 建筑列表
        build_list: [],

        // 道路列表
        road_list: [Number],

        // 士兵列表
        soldier_list: []
    },

    // 是否已经通过
    is_pass: {
        type: Boolean,
        default: false
    },

    // boss的伤害
    boss_damage: {
        type: Number,
        default: 0
    },

    // 创建时间
    create_time: {
        type: Date,
        default: Date.now
    }
});

/**
 * 获取战斗记录的编号
 */
bossRecordSchema.methods.getRecordId = function () {
    return this._id;
};

/**
 * 获取进攻方的角色id
 * @return {[type]} [description]
 */
bossRecordSchema.methods.getAtkRoleId = function () {
    return this.atk_role_id;
};

/**
 * 设置boss的伤害
 * @param {[type]} damage [description]
 */
bossRecordSchema.methods.setBossDamange = function (damage) {
    if (!!this.is_pass) {
        return;
    }
    this.is_pass = true;
    this.boss_damage = damage;
};

bossRecordSchema.methods.isPass = function () {
    return this.is_pass;
};

bossRecordSchema.plugin(autoIncrement.plugin, {
    model: 'bossRecordSchema',
    startAt: 10000
});

if (!bossRecordSchema.options.toJSON) {
    bossRecordSchema.options.toJSON = {};
}
/* jshint unused:false */
bossRecordSchema.options.toJSON.transform = function (doc, ret) {
    ret.create_time = moment(ret.create_time).unix();
    delete ret._id;
    delete ret.__v;
};

mongoose.model('BossRecord', bossRecordSchema);
